package xyz.duolaf;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import xyz.duolaf.dao.Usermapper;
import xyz.duolaf.pojo.User;

import java.io.InputStream;
import java.util.List;

public class Test {
    public static void main(String[] args) throws Exception {

        //1. 加载配置文件
        InputStream inputStream = Resources.getResourceAsStream("Mybatis-config.xml");

        //2. 通过SqlSessionFactoryBuilder加载配置文件创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //3. 通过sqlSessionFactory创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //4. 通过SqlSession创建mapper的实现对象
        Usermapper mapper = sqlSession.getMapper(Usermapper.class);

        //5. 调用mapper接口的方法
        List<User> userList = mapper.getUserListMapper();
        for (User user : userList) {
            System.out.println(user);
        }

        //6. 释放资源
        sqlSession.close();
    }
}
